import time
import requests
from selenium.webdriver.common.by import By

from common.Utils import TestDriver


# 测试问卷考试系统登录页面
class LoginTest:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver.get(self.url)
        self.driver.maximize_window()
        # 登录成功的测试用例

    def LoginSuccess(self):
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangzhang")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        TestDriver.getScreenshot()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        time.sleep(1)
        TestDriver.getScreenshot()
        # 测试是否跳转至首页
        assert self.driver.current_url == "http://8.155.1.153:8081/home"
        # 测试登录名称是否一致
        assert self.driver.find_element(By.CSS_SELECTOR,
                                        "#sk-layout > div > div > section > div.ant-pro-layout-container > header.ant-layout-header.ant-pro-layout-header.ant-pro-layout-header-fixed-header.ant-pro-layout-header-mix.ant-pro-layout-header-header > div > div.ant-pro-global-header-right-content > div > div > div > span > div > span:nth-child(2)").text == "zhangzhang"

    # 登录失败测试用例
    def LoginFail(self):
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#username"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#password"))
        # 测试正确的账号错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("test78")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        TestDriver.getScreenshot()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

        TestDriver.getScreenshot()

        assert self.driver.find_element(By.CSS_SELECTOR,
                                        "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text == "错误的用户名和密码"

